/**
 * 
 */
package com.efast.dmom.mes.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.mes.bean.Arc;
import com.efast.dmom.mes.bean.Route;
import com.efast.dmom.mes.bean.RouteQueue;
import com.efast.dmom.mes.dao.entity.RouteQueueDao;
import com.efast.dmom.mes.entity.RouteEntity;
import com.efast.dmom.mes.entity.RouteQueueEntity;
import com.efast.dmom.mes.entity.base.IModificationFlags;
import com.efast.dmom.mes.util.TimeUtil;

/**
 * 
 * @author Aaron Liu
 *
 *         2020年2月13日
 */
@Component
public class RouteQueueDataHandler {

	@Autowired
	private RouteQueueDao routeQueueDao;
	
	@Autowired
	private RouteArcDataHandler arcDataHandler = null;

	protected List<RouteQueue> saveQueueList(Route route, List<RouteQueue> queueList) {

		if(route == null || queueList == null || queueList.size() == 0){
			return new ArrayList<RouteQueue>();
		}
		
		RouteEntity routeEntity = route.getInnerData();
		
		int modificationFlag = 1;//routeEntity.getModificationFlag();
		if (IModificationFlags.UNCHANGED == modificationFlag) {
			return new ArrayList<RouteQueue>();
		}

		List<RouteQueueEntity> deletedEntityList = new ArrayList<RouteQueueEntity>();
		List<RouteQueueEntity> modifiedEntitydList = new ArrayList<RouteQueueEntity>();
		List<RouteQueueEntity> createdEntitydList = new ArrayList<RouteQueueEntity>();
		
		for (RouteQueue queue : queueList) {

			RouteQueueEntity innerData = queue.getInnerData();
			
			int queueModificationFlag = queue.getModificationFlag();

			if (IModificationFlags.NEW == queueModificationFlag) {

				innerData.setSiteId(DataHandlerUtils.getSiteId());
				innerData.setRouteId(routeEntity.getId());
				innerData.setCreationTime(TimeUtil.getLocalDateTime());
				innerData.setCreationTimeZone(TimeUtil.getLocalTimeZone());
				innerData.setCreator(SpringWebUtils.getRequestUser());
				innerData.setLastModifiedTime(TimeUtil.getLocalDateTime());
				innerData.setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
				innerData.setLastModifier(SpringWebUtils.getRequestUser());
//				innerData.setCreationPid(DataHandlerUtils.getDmomPid());
//				innerData.setLastModifiedPid(DataHandlerUtils.getDmomPid());
				innerData.setRowStatus(1);
				innerData.setRowTrxId(1);

				createdEntitydList.add(innerData);
			} 
			else if (IModificationFlags.DELETED == queueModificationFlag) {
				deletedEntityList.add(innerData);
			} 
			else if (IModificationFlags.MODIFIED == queueModificationFlag) {
				innerData.setLastModifiedTime(TimeUtil.getLocalDateTime());
				innerData.setLastModifier(SpringWebUtils.getRequestUser());
				innerData.setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
//				innerData.setLastModifiedPid(DataHandlerUtils.getDmomPid());	
				modifiedEntitydList.add(innerData);
			}
		}

		if (createdEntitydList.size() > 0) {
			routeQueueDao.saveQueueList(createdEntitydList);
		} 
		else if (deletedEntityList.size() > 0) {

			List<RouteQueue> tmpList = fromInnerData(deletedEntityList);
			removeRelatedArc(route, tmpList);
			
			routeQueueDao.removeQueueListById(deletedEntityList);
			route.removeRouteQueue(tmpList);
		} 
		else if (modifiedEntitydList.size() > 0) {
			routeQueueDao.updateQueueList(modifiedEntitydList);
		}
		
		return fromInnerData(createdEntitydList);
	}

	protected List<RouteQueue> fromInnerData(List<RouteQueueEntity> entityList) {

		List<RouteQueue> queueList = new ArrayList<RouteQueue>();

		for (RouteQueueEntity queueEntity : entityList) {

			RouteQueue queueBean = new RouteQueue();
			queueBean.setInnerData(queueEntity);

			queueList.add(queueBean);
		}
		return queueList;
	}
	
	public List<Arc> getRelatedArcs(Route route, RouteQueue routeQueue) {

		List<Arc> incommingArs = new ArrayList<Arc>();

		List<Arc> allArcs = route.getRouteArcs();
		for (Arc arc : allArcs) {

			String nodeUuid = routeQueue.getInnerData().getUuid();
			if ( arc.getInnerData().getToNodeUUID() ==  nodeUuid || arc.getInnerData().getFromNodeUUID() == nodeUuid) {

				incommingArs.add(arc);
			}
		}
		return incommingArs;
	}
	
	public void removeRelatedArc(Route route, List<RouteQueue> queueList) {

		List<Arc> arcList = new ArrayList<Arc>();

		for (RouteQueue queue : queueList) {
			arcList.addAll(getRelatedArcs(route, queue));
		}
		arcDataHandler.removeArcs(arcList);
		
		route.removeRouteArc(arcList);
	}
	
	protected List<RouteQueueEntity> getRouteQueueByRouteId(long routeId) {

		return routeQueueDao.getQueueByRouteId(routeId);
	}
	
	protected void removeQueueByRouteId(long routeId){
		routeQueueDao.removeQueueByRouteId(routeId);
	}

	protected void removeDeletedRouteQueues(List<RouteQueueEntity> entityList) {

		if (entityList == null) {
			return;
		}

		List<RouteQueueEntity> deletedList = new ArrayList<RouteQueueEntity>();

		for (RouteQueueEntity entity : entityList) {

			if (true //entity.getModificationFlag() == 1 //IModificationFlags.DELETED
			) {
				deletedList.add(entity);
			}
		}

		// routeQueueDao.
	}

}
